﻿/*
 * Last review by   : Yaser
 * Last review date : JUL-25-09 
 */
using System;
using System.Collections;
using ERP.Framework.Components.Utilities.TracingHelper;

namespace ERP.Framework.Components.SortingHelper
{
    /// <summary>
    /// Shell sort
    /// </summary>
    internal abstract class ShellSortHelper
    {
        #region Members

        private const string TRACING_CATEGORY = "ERP.Framework.Components.SortingHelper.ShellSortHelper";

        #endregion

        #region Methods

        /// <summary>
        /// Sort list
        /// </summary>
        /// <param name="array">list to sort</param>
        public static void SortList(IList array)
        {
            TraceUtil.Write(TRACING_CATEGORY, "SortList : Start");
            Comparer comparer = new Comparer(System.Threading.Thread.CurrentThread.CurrentCulture);

            for (int increment = array.Count / 2; increment > 0;
                   increment = (increment == 2 ? 1 : (int)Math.Round(increment / 2.2)))
            {
                for (int i = increment; i < array.Count; i++)
                {
                    object temp = array[i];
                    int j;
                    for (j = i; j >= increment && comparer.Compare(array[j - increment], temp) > 0; j -= increment)
                    {
                        array[j] = array[j - increment];
                    }
                    array[j] = temp;
                }
            }
            TraceUtil.Write(TRACING_CATEGORY, "SortList : End");
        }

        #endregion
    }
}
